<script setup lang="ts">
import { onMounted, ref } from 'vue'
import * as echarts from 'echarts'
import 'echarts-liquidfill'

const chartsRef = ref<any>()

const initCharts = () => {
  const myCharts = echarts.init(chartsRef.value)

  const options = {
    title: {
      text: '年龄分布',
      textStyle: {
        color: '#fff',
      },
      left: '20%',
      top: 'center',
    },
    tooltip: {
      trigger: 'item',
    },
    legend: {
      top: 'center',
      right: '50',
      orient: 'vertical',
      textStyle: {
        color: '#fff',
      },
    },
    series: [
      {
        name: '年龄',
        type: 'pie',
        center: ['30%', '50%'],
        radius: ['40%', '90%'],
        avoidLabelOverlap: false,
        itemStyle: {
          borderRadius: 10,
        },
        // 饼图文本标签
        label: {
          show: true,
          position: 'inside',
          formatter: '{d}%',
          color: '#fff',
        },
        // 高亮扇区
        emphasis: {
          label: {
            show: true,
            fontSize: 20,
            fontWeight: 'bold',
          },
        },
        labelLine: {
          show: false,
        },
        data: [
          { value: 16, name: '10岁以下' },
          { value: 8, name: '10-18岁' },
          { value: 12, name: '18-30岁' },
          { value: 24, name: '30-40岁' },
          { value: 20, name: '40-60岁' },
          { value: 20, name: '60岁以上' },
        ],
      },
    ],
  }
  myCharts.setOption(options)
}
onMounted(() => {
  initCharts()
})
</script>

<template>
  <div class="data">
    <div class="top">
      <p class="title">年龄比例</p>
    </div>
    <div class="charts" ref="chartsRef"></div>
  </div>
</template>

<style scoped lang="scss">
$title-height: 32px;

.data {
  flex: 1;

  .top {
    margin-bottom: 40px;
    font-size: 20px;
    line-height: $title-height;
    color: #fff;

    .title {
      &::after {
        top: $title-height;
        left: 0;
        display: block;
        width: 68px;
        height: 7px;
        content: '';
        background: url('@/assets/images/data_screen/figure.png') no-repeat;
        background-size: cover;
      }
    }
  }

  .charts {
    align-items: center;
    width: 100%;
    height: 220px;
  }
}
</style>
